package com.lenovo.agricultureclient.request;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;

import com.lenovo.agricultureclient.bean.SensorConfig;
import com.lenovo.agricultureclient.util.Util;

/**
 * 获取所有传感器阀值的请求
 * @author zongbingwu
 *
 */
public class GetConfigRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "getConfig";
	
	//存放所有传感器的阀值
	private SensorConfig sensorConfig;

	public SensorConfig getSensorConfig() {
		return sensorConfig;
	}
	public void setSensorConfig(SensorConfig sensorConfig) {
		this.sensorConfig = sensorConfig;
	}

	//action name
	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	//jason协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetJasonBody() 
	{
		return "{}"; //不能为空字符串
	}
	
	//soap协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetSoapBody() 
	{
		return Util.getSoapXml("");
	}
	
	//jason协议时解析回应函数
	@Override
	protected void onJasonParese(String responseStr) 
	{
		try 
		{
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if(jsonRespObj!=null && sensorConfig!=null)
			{
				if(jsonRespObj.has("result"))
				{
					String result = jsonRespObj.getString("result");
					result = result.toLowerCase();
					//判断请求是否成功
					if(result.equals("ok")){
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
				//解析CO2浓度最大值
				if(jsonRespObj.has("maxCo2")){
					sensorConfig.maxCo2 = jsonRespObj.getInt("maxCo2");
				}
				//解析灯光强度最大值
				if(jsonRespObj.has("maxLight")){
					sensorConfig.maxLight = jsonRespObj.getInt("maxLight");
				}
				//解析CO2浓度最小值
				if(jsonRespObj.has("minCo2")){
					sensorConfig.minCo2 = jsonRespObj.getInt("minCo2");
				}
				//解析灯光强度最小值
				if(jsonRespObj.has("minLight")){
					sensorConfig.minLight = jsonRespObj.getInt("minLight");
				}
				//解析土壤湿度最大值
				if(jsonRespObj.has("maxSoilHumidity")){
					sensorConfig.maxSoilHumidity = jsonRespObj.getInt("maxSoilHumidity");
				}
				//解析土壤湿度最小值
				if(jsonRespObj.has("minSoilHumidity")){
					sensorConfig.minSoilHumidity = jsonRespObj.getInt("minSoilHumidity");
				}
				//解析空气湿度最小值
				if(jsonRespObj.has("minAirHumidity")){
					sensorConfig.minAirHumidity = jsonRespObj.getInt("minAirHumidity");
				}
				//解析空气湿度最大值
				if(jsonRespObj.has("minAirTemperature")){
					sensorConfig.minAirTemperature = jsonRespObj.getInt("minAirTemperature");
				}
				//解析空气湿度最大值
				if(jsonRespObj.has("maxAirHumidity")){
					sensorConfig.maxAirHumidity = jsonRespObj.getInt("maxAirHumidity");
				}
				//解析空气温度最大值
				if(jsonRespObj.has("maxAirTemperature")){
					sensorConfig.maxAirTemperature = jsonRespObj.getInt("maxAirTemperature");
				}
				//0表示手动控制，1表示自动控制
				if(jsonRespObj.has("controlAuto")){
					sensorConfig.controlAuto = jsonRespObj.getInt("controlAuto");
				}
				//解析土壤温度最大值
				if(jsonRespObj.has("maxSoilTemperature")){
					sensorConfig.maxSoilTemperature = jsonRespObj.getInt("maxSoilTemperature");
				}
				//解析土壤温度最小值
				if(jsonRespObj.has("minSoilTemperature")){
					sensorConfig.minSoilTemperature = jsonRespObj.getInt("minSoilTemperature");
				}
				//解析PM2.5浓度最大值
				if(jsonRespObj.has("maxPM25")){
					sensorConfig.maxPM25 = jsonRespObj.getInt("maxPM25");
				}
			}
			else {
				isSuccess = false;
			}
		} 
		catch (JSONException e) 
		{
			isSuccess = false;
			e.printStackTrace();
		}
	}
	
	//soap协议时解析回应函数
	@Override
	protected void onSoapParese(String responseStr) 
	{
		//从完整的soap协议内容中解析出soap body内容
		Element soapBodyEle = Util.getSoapBodyElement(responseStr);
		if(soapBodyEle!=null && sensorConfig!=null)
		{
			try 
			{
				String result = Util.getChildElementValueStr(soapBodyEle, "result");
				result = result.toLowerCase();
				//判断请求是否成功
				if(result.equals("ok")){
					isSuccess = true;
				} else {
					isSuccess = false;
				}
				//解析空气温度最小值
				Element minAirT = (Element)soapBodyEle.getElementsByTagName("minAirTemperature").item(0);
				if(minAirT != null){
					sensorConfig.minAirTemperature = Integer.parseInt(minAirT.getFirstChild().getNodeValue());
				}
				//解析空气温度最大值
				Element maxAirT = (Element)soapBodyEle.getElementsByTagName("maxAirTemperature").item(0);
				if(maxAirT!=null){
					sensorConfig.maxAirTemperature = Integer.parseInt(maxAirT.getFirstChild().getNodeValue());
				}
				//解析空气湿度最小值
				Element minAirH = (Element)soapBodyEle.getElementsByTagName("minAirHumidity").item(0);
				if(minAirH!=null){
					sensorConfig.minAirHumidity = Integer.parseInt(minAirH.getFirstChild().getNodeValue());
				}
				//解析空气湿度最大值
				Element maxAirH = (Element)soapBodyEle.getElementsByTagName("maxAirHumidity").item(0);
				if(maxAirH!=null){
					sensorConfig.maxAirHumidity = Integer.parseInt(maxAirH.getFirstChild().getNodeValue());
				}
				//解析土壤温度最小值
				Element minSoilT = (Element)soapBodyEle.getElementsByTagName("minSoilTemperature").item(0);
				if(minSoilT!=null){
					sensorConfig.minSoilTemperature = Integer.parseInt(minSoilT.getFirstChild().getNodeValue());
				}
				//解析土壤温度最大值
				Element maxSoilT = (Element)soapBodyEle.getElementsByTagName("maxSoilTemperature").item(0);
				if(maxSoilT!=null){
					sensorConfig.maxSoilTemperature = Integer.parseInt(maxSoilT.getFirstChild().getNodeValue());
				}
				//解析土壤湿度最小值
				Element minSoilH = (Element)soapBodyEle.getElementsByTagName("minSoilHumidity").item(0);
				if(minSoilH!=null){
					sensorConfig.minSoilHumidity = Integer.parseInt(minSoilH.getFirstChild().getNodeValue());
				}
				//解析土壤湿度最大值
				Element maxSoilH = (Element)soapBodyEle.getElementsByTagName("maxSoilHumidity").item(0);
				if(maxSoilH!=null){
					sensorConfig.maxSoilHumidity = Integer.parseInt(maxSoilH.getFirstChild().getNodeValue());
				}
				//解析灯光强度最小值
				Element minLihgt = (Element)soapBodyEle.getElementsByTagName("minLight").item(0);
				if(minLihgt!=null){
					sensorConfig.minLight = Integer.parseInt(minLihgt.getFirstChild().getNodeValue());
				}
				//解析灯光强度最大值
				Element maxLihgt = (Element)soapBodyEle.getElementsByTagName("maxLight").item(0);
				if(maxLihgt!=null){
					sensorConfig.maxLight = Integer.parseInt(maxLihgt.getFirstChild().getNodeValue());
				}
				//解析CO2浓度最小值
				Element minCo2 = (Element)soapBodyEle.getElementsByTagName("minCo2").item(0);
				if(minCo2!=null){
					sensorConfig.minCo2 = Integer.parseInt(minCo2.getFirstChild().getNodeValue());
				}
				//解析CO2浓度最大值
				Element maxCo2 = (Element)soapBodyEle.getElementsByTagName("maxCo2").item(0);
				if(maxCo2!=null){
					sensorConfig.maxCo2 = Integer.parseInt(maxCo2.getFirstChild().getNodeValue());
				}
				//解析PM2.5浓度最大值
				Element maxPm25 = (Element)soapBodyEle.getElementsByTagName("maxPM25").item(0);
				if(maxPm25!=null){
					sensorConfig.maxPM25 = Integer.parseInt(maxPm25.getFirstChild().getNodeValue());
				}
				//0表示手动控制，1表示自动控制
				Element controlAuto = (Element)soapBodyEle.getElementsByTagName("controlAuto").item(0);
				if(controlAuto!=null){
					sensorConfig.controlAuto = Integer.parseInt(controlAuto.getFirstChild().getNodeValue());
				}
			} catch (Exception e) {
				e.printStackTrace();
				isSuccess = false;
			}
		} else {
			isSuccess = false;
		}
	}

}
